<template>
  <div class="app-container">

    <el-form :model="form" ref="form" label-width="160px" v-loading="formLoading" :rules="rules">
       <el-form-item label="讲师姓名：" prop="lecturerName" required>
        <el-input v-model="form.lecturerName"></el-input>
      </el-form-item>
      <el-form-item label="联系方式：" prop="phone" required>
        <el-input v-model="form.phone"></el-input>
      </el-form-item>
      <!--性别和讲师职称改为下拉列表的形式-->
      <el-form-item label="性别：" prop="gender" required>
         <el-select v-model="form.gender" placeholder="性别" clearable>
            <el-option
                v-for="item in this.sexList"
                :key="item.id"
                :label="item.sex"
                :value="item.id"
              ></el-option>

        </el-select>
      </el-form-item>
      <!--<el-form-item label="讲师职称：">
        <el-dropdown trigger="click" v-model="form.lecturerTitle">
          <span class="el-dropdown-link">
            请选择
            <i class="el-icon-caret-bottom el-icon--right"></i>
          </span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item>教授</el-dropdown-item>
            <el-dropdown-item>副教授</el-dropdown-item>
            <el-dropdown-item>讲师</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </el-form-item>-->
      <el-form-item label="讲师职称：" prop="professionalTitle">
        <el-select v-model="form.professionalTitle" placeholder="讲师职称" clearable>
          <el-option
            v-for="item in lecturertitleList"
            :key="item.id"
            :value="item.id"
            :label="item.titleName"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="讲师详情：" prop="introduction">
        <el-input v-model="form.introduction"></el-input>
      </el-form-item>
      <el-form-item label="讲师标签：" prop="label">
        <el-input v-model="form.label"></el-input>
      </el-form-item>
      <!--头像上传(从本地文件中选择)-->
      <el-form-item label="头像上传：" required>
        <el-upload
          class="upload-demo"
          action="http://175.24.33.88:8000/api/admin/course/courseCoverUpload"
          :on-preview="handlePreview"
          ref="upload"
          :on-remove="handleRemove"
          :before-remove="beforeRemove"
          multiple
          :auto-upload="true"
          :http-request="fileUpload"
          :with-credentials="true"
        >
          <el-button size="small" type="primary">点击上传</el-button>
          <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
        </el-upload>
        <img v-show= imgshow width="40%" height="400px"   :src="'http://175.24.33.88:8001/static'+this.form.photoPath" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">提交</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>

    </el-form>


  </div>
</template>

<script>
import {selectLecturer,createLecturer} from "@/api/xzs/lecturer";
import {userApi} from "@/api/system/user";
import {courseApi} from "@/api/xzs/course";
import {listUser} from "@/api/category/lecturetitle";
import {form} from "@/utils/request"
import request from '@/utils/request'
import Editor from '@/components/Editor';
export default {
  inject:['reload'],
   components: {
    Editor
  },
  data() {
    return {
      form: {
        id: null,
        lecturerName: "",
        phone: null,
        gender: "",
        professionalTitle: "",
        introduction: "",
        label: "",
        photoPath: "",
        pageNum: 1,
        pageSize: 10,
        titleName: ""
      },
      imgshow:false,
       sexList:[
          {sex: '男', id: 1},
          {sex: '女', id: 2}
      ],
      lecturertitleList:[],
      formLoading: false,
      rules: {
         className: [
          { required: true, message: '请输入班级名称', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    let id = this.$route.query.id
    let _this = this
    this.getLecturer();
    if (id && parseInt(id) !== 0) {
      _this.formLoading = true
      this.imgshow=true;
      selectLecturer(id).then(re => {
        _this.form = re.response
        // console.log(this.form.photoPath)
        // console.log(this.imgshow)
        _this.formLoading = false
      })
    }
  },
  methods: {
    fileUpload(file){
      let files = file.file
      //console.log(file.file)
      let formObj = new FormData();
      formObj.append("uploadFile", files)
      request({
        method: "post",
        url: "/api/admin/course/courseCoverUpload", // 请求地址
        data: formObj, // 参数
        //responseType: "blob", // 表明返回服务器返回的数据类型
        headers: {
          "Content-Type": "application/json"
        }
      }).then(res => {
      //request('/api/admin/course/courseCoverUpload',formObj).
        //console.log(res);
        this.form.photoPath = res.response;
        this.imgshow=true;
      })
    },
      submitForm () {
      let _this = this
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.formLoading = true
          createLecturer(this.form).then(data => {
            if (data.code === 1) {
              _this.$message.success(data.message)
              _this.$router.push('/courses/lecturer/index')

            } else {
              _this.$message.error(data.message)
              _this.formLoading = false
            }
          }).catch(e => {
            _this.formLoading = false
          })
        } else {
          return false
        }
      })
    },
    getLecturer() {

      this.formLoading=true;
      listUser(this.form)
        .then(res => {
          this.formLoading=false;
          if (res.code == 1) {
            this.lecturertitleList = res.response.list;
          }
        })
        .catch(res => {
          this.formLoading=false;
        });
    },
     handleRemove(file,uploadFile) {
      this.form.photoPath=null;
      this.imgshow=false;
      //console.log(file);
    },
    handlePreview(file) {
      //console.log(file);
    },
    //handleExceed(files, fileList) {
      //this.$message.warning(
        //`当前限制选择3个文件，本次选择了${
          //files.length
       // }个文件，共选择了${files.length + fileList.length}个文件`
     // );
   // },
    beforeRemove(file) {
      return this.$confirm(`确定移除?`);

    },
    resetForm () {
      this.reload()
    }
  }
};
</script>
